// string.go
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type DrString struct {
	DrAbstractObject
	ISequence
	strValue string
}

func (self *DrString) getDouble() float64 {
	v, _ := strconv.ParseFloat(self.strValue, 64)
	return v
}
func (self *DrString) getInt() int {
	v, _ := strconv.ParseInt(self.strValue, 10, 64)
	return int(v)
}

func NewStr(s string) *DrString {
	str := new(DrString)
	str.Type = TypeString

	str.strValue = s

	str.members = make(map[string]IObject)
	str.members["split"] = NewFunction("split", []string{"sep"})

	return str
}
func (self *DrString) getMembers() map[string]IObject {
	return self.members
}
func (self *DrString) executeMethod(funcName string, prams []IObject) IObject {
	if funcName == "split" {
		var sep string
		if len(prams) == 2 {
			sep = prams[1].(*DrString).strValue
			return self.Split(sep)
		} else if len(prams) == 1 {
			strs := self.Split(" ")
			str2 := self.Split("\t")
			if strs.Len() > str2.Len() {
				return strs
			} else {
				return str2
			}
		}
	}

	panic("not implemented for the method:" + funcName)

}
func (self *DrString) Split(sep string) *DrStrArray {
	strs := strings.Split(self.strValue, sep)

	return NewStrArray1(strs)
}
func (self *DrString) value() string {
	return self.AsString()
}
func (self *DrString) Len() int {
	return len(self.strValue)
}
func (self *DrString) GetValue(index int) string {
	return string(self.AsString()[index])
}
func (self *DrString) AsString() string {
	return self.strValue
}
func (self *DrString) Print(interp *Interpreter) {
	fmt.Print(self.AsString())
}
func (self *DrString) Add(str *DrAbstractObject) IObject {
	s := self.AsString() + str.AsString()

	result := NewStr(s)
	return result
}

func (self *DrString) getType() DataType {
	return self.Type
}
